import { FairyGUI } from 'csharp';
import UrlUtils from '../com/utils/UrlUtils';
/**
 * 面板注册 clong 2019.12.3
 */
export default class PanelRegister{

    private static pool:Object = {};
    private static poolCount:Object = {};

    /**面板panelvo字典 */
    private static panelVoMap:Object = {};
    private static panelIdIndex:number = 0;

    constructor() {

    }

    /**
     * 
     * @param pkgName 是否已注册资源包
     */
    public static hasRegisterClass( pkgName:string ):boolean{

        return pkgName && !FairyGUI.UIPackage.GetById(UrlUtils.FAIRUI+pkgName);
    }
    
    /**
     * 注册组件类与fairygui编辑器中类对应
     * @param pkgName 包名
     * @param resName 资源名
     * @param cls	  对应包中类名	
     */
    public static registerClass(pkgName: string, resName: string, cls: any): void {

        // let pkgs = FairyGUI.UIPackage.GetPackages();
        // for(let i=0;i<pkgs.Count;i++){
        //     let pkg: FairyGUI.UIPackage = pkgs.get_Item(i);
        //     if( pkg ){
        //         console.log( "--------->[registerClass] pkg.name: " + pkg.name + " pkg.id: " + pkg.id + " pkg.customId: " + pkg.customId);
        //     }
        // }
        // if (pkgName && !FairyGUI.UIPackage.GetByName(pkgName)) {            
        //     FairyGUI.UIPackage.AddPackage( UrlUtils.FAIRUI + pkgName);
        // }
        let url: string = FairyGUI.UIPackage.GetItemURL(pkgName, resName);

        console.log("---------->registerClass pkgName: "+pkgName + " resName: " + resName + " url: " + url );

        FairyGUI.UIObjectFactory.SetPackageItemExtension(url, cls);
    }
    
    /**
     * 创建自定义fairygui组件，必须用此方式,与以上方法对应使用,不能直接使用new cls()的方式创建一个绑定fairygui的类！
     * @param pkgName 包名
     * @param resName 资源名
     */
    public static createGObject(pkgName: string, resName: string , path:string = "" ): any {

        let obj:FairyGUI.GObject = null;
        let _id = pkgName+"_"+resName;
        let arr:Array<any> = this.pool[ _id ];
        if( arr && arr.length > 0 ){
            obj = arr.shift();
            // obj.displayObject.active = true;
            return obj;
        }
        let count = this.poolCount.hasOwnProperty( _id ) ? this.poolCount[ _id ] : 0;
        let url = this.getUrl( pkgName , resName , path );
        obj = FairyGUI.UIPackage.CreateObjectFromURL( url );//FairyGUI.UIPackage.getItemURL(pkgName, resName)
        obj.id = _id + "_" + count;       
        count++;
        this.poolCount[ _id ] = count; 
        // obj.displayObject.active = true;
        return obj;
    }

    public static recover( obj:FairyGUI.GObject ):void{

        if( obj != null ){
            let _id = obj.id.substr( 0 , obj.id.lastIndexOf("_") );
            let arr:Array<any> = this.pool[ _id ];
            if( arr == null ){
                arr = [];
                this.pool[_id] = arr;
            }
            // obj.displayObject.active = false;
            obj.RemoveFromParent();
            obj.alpha = 1;
            obj.visible = true;
            arr.push( obj );
        }
    }

    /**
     * 获取资源地址
     * @param pkgName   包名
     * @param resName   资源名
     * @param path      包下面的路径 
     */
    public static getUrl( pkgName:string , resName:string , path:string = "" ):string{

        let url:string = FairyGUI.UIPackage.GetItemURL( pkgName , resName );
        return url;
    }

    /**
     * 获取对应的资源
     * @param pkgName 包名
     * @param resName 资源名
     */
    public static getItemAsset(  pkgName:string , resName:string ):any{

        let url:string = FairyGUI.UIPackage.GetItemURL(pkgName, resName);
        return FairyGUI.UIPackage.GetItemAssetByURL( url )
    }

    /**
     * 获取Fairy资源包里的图片资源
     * @param pkgName   包名
     * @param resName   资源名
     */
    public static getTextureBy(pkgName:string , resName:string , path:string = "" ):FairyGUI.NTexture{

        let url:string = this.getUrl( pkgName , resName , path );
        return this.getTexture( url );
    }

    /**
     * 根据Fairygui的URL地址获取对应纹理
     * @param uiUrl     如ui://q4evlwcjdmoc2i
     */
    public static getTexture( url:string ):FairyGUI.NTexture{

        let item:FairyGUI.PackageItem = FairyGUI.UIPackage.GetItemByURL( url );
        if( item ){
            item.Load();
        }
        return item ? item.texture : null;
    }
}